﻿<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public class Handler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        string strConn = UserClass.strconn;
        using (SqlConnection conn = new SqlConnection(strConn))
        {
            //int count = context.Request.QueryString["count"];
            int count = 5;
            string table = "compare_"+count+"_products";
            List<int> cpList = new List<int>();
            string strCmd1 = @"SELECT TOP 5 cp.*, pp.category_id
                                                    FROM " +table+ @" as cp INNER JOIN
                                                    production_product as pp ON cp.product_1 = pp.product_id
                                                    WHERE category_id = @c_id
                                                    ORDER BY [count] DESC";
            using (SqlCommand cmd = new SqlCommand(strCmd1, conn))
            {
                cmd.Parameters.AddWithValue("@c_id", 3);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    for (int i = 1; i <= count; i++)
                    {
                        cpList.Add(Convert.ToInt32(reader[i]));
                    }
                }
                conn.Close();
            }

            List<ProductPreview> pps = new List<ProductPreview>();
            string prosId = cpList[0].ToString();
            for (int i = 1; i < cpList.Count; i++)
            {
                prosId = prosId + "," + cpList[i].ToString();
            }
            string strCmd2 = @"SELECT production_product.product_id AS id, production_product.product_name AS name, production_image.[image]
                                                FROM production_product INNER JOIN
                                                           production_image ON production_product.product_id = production_image.product_id AND 
                                                           production_product.product_id = production_image.product_id
                                                WHERE (production_image.image_order = 1) AND production_image.product_id in ("+prosId+")";
            using (SqlCommand cmd = new SqlCommand(strCmd2, conn))
            {
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ProductPreview pp = new ProductPreview();
                    pp.ID = Convert.ToInt32(reader["id"]);
                    pp.Name = reader["name"].ToString();
                    pp.Image = (byte[])reader["image"];
                    pps.Add(pp);
                }
                context.Response.ContentType = "application/json";
                context.Response.Write(JsonConvert.SerializeObject(pps));
                conn.Close();
            }
        }
    }

    private byte[] ObjectToByteArray(Object obj)
    {
        if (obj == null)
            return null;
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream ms = new MemoryStream();
        bf.Serialize(ms, obj);
        return ms.ToArray();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}